# coding=utf-8
import xlrd

from code_builder.conf import read_conf

class read_xls(object):
    def __init__(self):
        self.file_path = read_conf.getConfig("path","excel_file_path")

    def get_data(self):
        data = xlrd.open_workbook(self.file_path)
        result = {}
        if len(data.sheets()) != 0:
            #  遍历表格
            for i in range(len(data.sheets())):
                table = data.sheets()[i]
                # 获取表格名称
                table_name = table.name.upper()
                # 获取表格行数
                rows_num = table.nrows
                # 获取表格纵数
                cols_num = table.ncols
                rows = []
                if rows_num != 0 and cols_num != 0:
                    for j in range(rows_num):
                        row = {}
                        tmp_list = table.row_values(j)
                        row['field_name'] = tmp_list[0]
                        row['field_code'] = tmp_list[1].replace(" ","")
                        row['field_type'] = tmp_list[2]
                        row['field_len'] = tmp_list[3]
                        row['field_dec'] = tmp_list[4]
                        row['field_restrain'] = tmp_list[5]
                        row['field_mark'] = tmp_list[6]
                        rows.append(row)
                    result[table_name] = rows
                else:
                    continue
        return result

if __name__=="__main__":
    a = read_xls()
    print(a.get_data())